﻿using SwenAssigment.Classes;
using SwenAssigment.DBClasses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class StaffOverview : System.Web.UI.Page
    {
        private int DeleteButtonIndex = 9;
        private int IdLabelIndex = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.Visible = false;
                NoDataFoundLbl.Visible = false;
                GridView1.DataSource = StaffDBManager.GetAllStaff();
                GridView1.DataBind();
                if (GridView1.Rows.Count != 0)
                {
                    GridView1.Visible = true;
                }
                else
                {
                    NoDataFoundLbl.Visible = true;
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinkButton DeleteInnerLinkButton = ((LinkButton)GridView1.Rows[e.RowIndex].Cells[DeleteButtonIndex].Controls[0]);
            string ConfirmStr = DeleteInnerLinkButton.Text.Replace("Delete", "Confirm");
            if (DeleteInnerLinkButton.Text != ConfirmStr)
            {
                DeleteInnerLinkButton.Text = ConfirmStr;
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
                Staff TempStaff = StaffDBManager.GetStaffById(Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[IdLabelIndex].Text));
                if (StaffDBManager.GetStaffByGeneralDuty(TempStaff.StaffId) == true)
                {
                    StaffDBManager.DeleteStaffByGeneralDuty(TempStaff.StaffId);
                }
                if (StaffDBManager.GetStaffByRoomDuty(TempStaff.StaffId) == true)
                {
                    StaffDBManager.DeleteStaffByRoomDuty(TempStaff.StaffId);
                }
                if (StaffDBManager.GetStaffByEstateDuty(TempStaff.StaffId) == true)
                {
                    StaffDBManager.DeleteStaffByEstateDuty(TempStaff.StaffId);
                }
                if (StaffDBManager.GetStaffBySecurityDuty(TempStaff.StaffId) == true)
                {
                    StaffDBManager.DeleteStaffBySecurityDuty(TempStaff.StaffId);
                }
                StaffDBManager.DeleteStaff(TempStaff.StaffId);
                Response.Redirect("~/StaffSuccessfulDelete.aspx");
                //GridView1.DataSource = StaffDBManager.GetAllStaff();
                //GridView1.DataBind();
            }
        }
    }
}